package com.food.eat.impl;

import com.food.eat.mapper.CategoryMapper;
import com.food.eat.mapper.CategoryMapperCustom;
import com.food.eat.pojo.Category;
import com.food.eat.pojo.vo.CategoryVo;
import com.food.eat.pojo.vo.NewItemsVo;
import com.food.eat.service.CategoryService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * @Author:RenPu
 * @Date: 2020/7/21 10:11
 * @Version: 1.0
 * @description:
 */

@Service
public class CategoryServiceimpl implements CategoryService {


    @Resource
    private CategoryMapper categoryMapper;

    @Resource
    private CategoryMapperCustom categoryMapperCustom;


    /**
     * 获取一级类别数据的实现
     *
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<Category> queryByTypeFirstCate() {

        Example example = new Example(Category.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("type", 1);
        List<Category> categories = categoryMapper.selectByExample(example);
        return categories;
    }


    /**
     * 根据一级大类的的id值获取其子类
     *
     * @param rootCatId
     * @return
     */

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<CategoryVo> querySubCate(Integer rootCatId) {
//        categoryMapperCustom.interType()  //调用接口中设置default 方法【java8 新特性】
        return categoryMapperCustom.getSubList(rootCatId);
    }


    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<NewItemsVo> getNewSixItems(Integer rootCatId) {

        HashMap<String, Object> map = new HashMap<>();
        map.put("rootCatId", rootCatId);
        return categoryMapperCustom.getNewSixItems(map);
    }
}
